﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TVKWD2PR
    {
        public TVKWD2PR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWDECK();
            this.DataContext = MV;
            MESSAGES += "WDLOAD;WDRELOAD;MUREV;";
            InitializeComponent();
        }
        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                return;
            }

            RULEKEY = dwMaster["WDAUFKRS"].ToString() + dwMaster["WDAUFLFD"].ToString() + dwMaster["WDAUFPOS"].ToString() + dwMaster["WDUPOS"].ToString() + "VK ";
            ABAUF =  dwMaster["WDAUFKRS"].ToString() + dwMaster["WDAUFLFD"].ToString() + dwMaster["WDAUFPOS"].ToString() + dwMaster["WDUPOS"].ToString() + "PR ";

            if (dwRule == null)
            {
                TMV.VKWDPLAND(dwMaster);
            }
            if (double.Parse(dwRule["QTYEND"].ToString()) != double.Parse(dwMaster["WDBEDARF"].ToString())*0.01)
            {
                TMV.VKWDPLAND(dwMaster);
            }
        }
        /// <summary>
        ///每次计划，自动产生生产单号码
        /// </summary>
        public override void OnABChg(bool _IsAB)
        {
            if (!_IsAB)
                return;
            base.OnABChg(_IsAB);
        }
        public override void OnKRS()
        {
            base.OnKRS();
        }
        public void Preplan(DataRow dwWD)
        {
            switch(app.company)
            {
                case "UBT":
                    UBTPLAN(dwWD);
                    break;
                case "GTT":
                    GTTPLAN(dwWD);
                    break;
            }
        }
        public void GTTPLAN(DataRow dwWD)
        {
            string KDAUFTNR = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString();
            if (ABAUF == "")
            {
                PM["RUNID"] = this.RUNID;
                PM["KRS"] = ABKRS;
                PM["LFD"] = dwWD["WDAUFLFD"].ToString();
                PM["AUFART"] = AUFART;
                PM["ORDERNUM"] = KDAUFTNR;
                PM["RELOADABK"] = false;
                ABAUF = TMV.VABK.NEWABK(PM);
            }
            RUNID = this.RUNID;
            PM["KRS"] = ABAUF.Substring(0, 2);
            PM["LFD"] = ABAUF.Substring(2, 6);
            PM["AUFART"] = "VA";
            PM["ORDERNUM"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString();
            TMV.VABK.LOAD(PM);
            dwMaster["KRS"] = ABAUF.Substring(0, 2);
            dwMaster["LFD"] = ABAUF.Substring(2, 6);
            //POS = VSDDAT.SD22.GETNEXT("0022", ABKRS, ABAUF.Substring(0, 8), 1);
            dwMaster["POS"] = dwWD["WDAUFPOS"].ToString();
            PM["RUNID"] = RUNID;
            //根据不同点的原料类型，需求创建不同的订单
            //在创建时，决定使用现有库存还是新订单 
            if (dwWD == null)
                return;
            PM["ORDERNUM"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDSA"].ToString() + dwWD["WDUPOS"].ToString(); ;
            //销售单规则已经创建好，这里捕捉实际值，并保存规则
            if (dwRule == null)
                return;
            MessageInfo msg = new MessageInfo();
            #region===产生首个计划规则，传递参数======
            msg = new MessageInfo();
            msg.AUFTYP = "K";
            msg.WAKZ = dwWD["WDWAKZ"].ToString();
            msg.ART = dwWD["WDART"].ToString();
            msg.VAR = dwWD["WDVAR"].ToString();
            msg.FARBE = dwWD["WDFARBE"].ToString();
            msg.BESCHICH = dwWD["WDBESCHICH"].ToString();
            msg.DESS = dwWD["WDDESS"].ToString();
            msg.COLORIT = dwWD["WDCOLORIT"].ToString();
            msg.BESCHICH = dwWD["WDBESCHICH"].ToString();
            msg.DESS = dwWD["WDDESS"].ToString();
            msg.COLORIT = dwWD["WDCOLORIT"].ToString();
            msg.KRS = dwWD["WDAUFKRS"].ToString();
            msg.LFD = dwWD["WDAUFLFD"].ToString();
            msg.POS = dwWD["WDAUFPOS"].ToString();
            msg.P1 = dwWD["WDUPOS"].ToString();
            msg.P2 = dwWD["WDSA"].ToString();
            msg.Where = app.User;
            msg.Sender = this.RUNID;
            PM["CART"] = dwWD["WDART"].ToString() + dwWD["WDVAR"].ToString() + dwWD["WDFARBE"].ToString() + dwWD["WDBESCHICH"].ToString() + dwWD["WDDESS"].ToString() + dwWD["WDCOLORIT"].ToString();
            PM["VKPAUF"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDUPOS"].ToString() + "K";
            PM["VKAUF"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDUPOS"].ToString() + "K";
            msg.MSGTYPE = "RZABPNEW";
            msg.ACTION = "RZABPNEW";
            PM["ARTSTAGE"] = "F";
            PM["DELDATE"] = dwRule["DELDATE"].ToString();
            PM["VKPQTY"] = dwRule["QTYEND"].ToString();
            PM["ORDERNUM"] = dwWD["KRS"].ToString() + dwWD["LFD"].ToString() + dwWD["POS"].ToString() + "VA ";
            PM["COLOR"] = "VA ";
            PM["AUFTYP"] = "V";
            PM["QTYEND"] = dwRule["QTYMAT"].ToString();
            PM["P7"] = PM["DELDATE"];//VKDELDATE
            PM["P10"] = dwWD["WAFZUART"].ToString();
            PM["JNUM"] = 1;//匹数
            PM["JWASTGE"] = 10;//接缝损耗
            PM["P12"] = 40;//头尾损耗
            double restqty = double.Parse(dwRule["QTYEND"].ToString());
            PM["P11"] = 3;
            if (restqty<=100)
            {
                PM["P11"] = 15; // 根据订单量的损耗
            }
            else
            {
                if (restqty <= 300)
                {
                    PM["P11"] = 10; // 根据订单量的损耗

                }
                else
                {
                    if (restqty <= 500)
                    {
                        PM["P11"] = 8; // 根据订单量的损耗

                    }
                    else
                    {
                        if (restqty <= 1200)
                        {
                            PM["P11"] = 5; // 根据订单量的损耗 
                        } 
                    }
                }
            }

            TMV.VPR.NEWPLAN(PM);
            #endregion
            PM["NEWABK"] = "F";
            msg.P3 = (string)PM["NEWABK"];
            msg.ORDER = (string)PM["ORDERNUM"];
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }
        public void UBTPLAN(DataRow dwWD)
        {
            string KDAUFTNR = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString();
            if (ABAUF == "")
            {
                PM["RUNID"] = this.RUNID;
                PM["KRS"] = ABKRS;
                PM["LFD"] = dwWD["WDAUFLFD"].ToString();
                PM["AUFART"] = AUFART;
                PM["ORDERNUM"] = KDAUFTNR;
                PM["RELOADABK"] = false;
                ABAUF = TMV.VABK.NEWABK(PM);
            }
            RUNID = this.RUNID;
            PM["KRS"] = ABAUF.Substring(0, 2);
            PM["LFD"] = ABAUF.Substring(2, 6);
            PM["AUFART"] = "VA";
            PM["ORDERNUM"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString();
            TMV.VABK.LOAD(PM);
            dwMaster["KRS"] = ABAUF.Substring(0, 2);
            dwMaster["LFD"] = ABAUF.Substring(2, 6);
            //POS = VSDDAT.SD22.GETNEXT("0022", ABKRS, ABAUF.Substring(0, 8), 1);
            dwMaster["POS"] = dwWD["WDAUFPOS"].ToString();
            PM["RUNID"] = RUNID;
            //根据不同点的原料类型，需求创建不同的订单
            //在创建时，决定使用现有库存还是新订单 
            if (dwWD == null)
                return;
            PM["ORDERNUM"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDSA"].ToString() + dwWD["WDUPOS"].ToString(); ;
            //销售单规则已经创建好，这里捕捉实际值，并保存规则
            if (dwRule == null)
                return;
            MessageInfo msg = new MessageInfo();
            #region===产生首个计划规则，传递参数======
            msg = new MessageInfo();
            msg.AUFTYP = "K";
            msg.WAKZ = dwWD["WDWAKZ"].ToString();
            msg.ART = dwWD["WDART"].ToString();
            msg.VAR = dwWD["WDVAR"].ToString();
            msg.FARBE = dwWD["WDFARBE"].ToString();
            msg.BESCHICH = dwWD["WDBESCHICH"].ToString();
            msg.DESS = dwWD["WDDESS"].ToString();
            msg.COLORIT = dwWD["WDCOLORIT"].ToString();
            msg.BESCHICH = dwWD["WDBESCHICH"].ToString();
            msg.DESS = dwWD["WDDESS"].ToString();
            msg.COLORIT = dwWD["WDCOLORIT"].ToString();
            msg.KRS = dwWD["WDAUFKRS"].ToString();
            msg.LFD = dwWD["WDAUFLFD"].ToString();
            msg.POS = dwWD["WDAUFPOS"].ToString();
            msg.P1 = dwWD["WDUPOS"].ToString();
            msg.P2 = dwWD["WDSA"].ToString();
            msg.Where = app.User;
            msg.Sender = this.RUNID;
            PM["CART"] = dwWD["WDART"].ToString() + dwWD["WDVAR"].ToString() + dwWD["WDFARBE"].ToString() + dwWD["WDBESCHICH"].ToString() + dwWD["WDDESS"].ToString() + dwWD["WDCOLORIT"].ToString();
            PM["VKPAUF"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDUPOS"].ToString() + "K";
            PM["VKAUF"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDUPOS"].ToString() + "K";
            msg.MSGTYPE = "RZABPNEW";
            msg.ACTION = "RZABPNEW";
            PM["ARTSTAGE"] = "F";
            PM["DELDATE"] = dwRule["DELDATE"].ToString();
            PM["VKPQTY"] = dwRule["QTYEND"].ToString();
            PM["ORDERNUM"] = dwWD["KRS"].ToString() + dwWD["LFD"].ToString() + dwWD["POS"].ToString() + "VA ";
            PM["COLOR"] = "VA ";
            PM["AUFTYP"] = "V";
            PM["QTYEND"] = dwRule["QTYMAT"].ToString();
            PM["P7"] = PM["DELDATE"];//VKDELDATE
            PM["P10"] = dwWD["WAFZUART"].ToString();
            TMV.VPR.NEWPLAN(PM);
            #endregion
            PM["NEWABK"] = "F";
            msg.P3 = (string)PM["NEWABK"];
            msg.ORDER = (string)PM["ORDERNUM"];
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }
        public void RemoveCommand(object sender, RoutedEventArgs e)
        {
            if (txtDel.Text == "N")
            {
                MessageBox.Show("请手工把标志设置为Y");
                return;
            }
            if (dwMaster == null)
                return;
            PM["KRS"]= dwMaster["WDAUFKRS"].ToString();
            PM["LFD"]= dwMaster["WDAUFLFD"].ToString();
            PM["POS"]= dwMaster["WDAUFPOS"].ToString();
            TMV.DVKPON(PM);
            txtDel.Text = "N";
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ABAUF = "";
            Preplan(dwMaster);
            base.NewCommand(sender, e);
        }

        private void WZCommand(object sender, RoutedEventArgs e)
        {

        }
    }
}
